require "luajit-stdlib.__tableUtil";
local _ffi = require "ffi"
ffi = table.extend(_ffi);
ffi.cdef(
    [[
        typedef unsigned long DWORD;
        typedef const wchar_t* utf8str;
        typedef const char* str;
        typedef long BOOL;
        typedef void* ptr;
        typedef const char *LPCCH,*PCSTR,*LPCSTR;
        typedef void *HANDLE;
        typedef HANDLE HMODULE;
        
        void Sleep(int ms);
        int printf(str format, ...);
        int GetTickCount();
        void* malloc(size_t size);
        void* free(void* memblock);
        
        int MultiByteToWideChar(
            int CodePage,
            int dwFlags,
            str lpMultiByteStr,
            int cchMultiByte,
            utf8str lpWideCharStr,
            int cchWideChar
        );
    ]]
)

local msvcrt = ffi.load("msvcrt")
function ffi.malloc(size)
    return msvcrt.malloc(size)
end
function ffi.free(cdata)
    return msvcrt.free(cdata)
end
function ffi.getPtr(p)
    return ffi.cast("intptr_t", p)
end
function ffi.ptr2int(ptr)
    return ffi.cast("int", ptr)
end
function ffi.buff(len)
    return ffi.new("char[?]", len)
end